package com.example.module_user

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.animation.LinearInterpolator
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_base.BaseActivity
import com.example.lib_http.RetrofitManager
import com.example.module_user.databinding.ActivityUserBinding
import com.example.module_user.intent.UserIntent
import com.example.module_user.state.UserState
import com.example.module_user.viewmodel.UserViewModel
import kotlinx.coroutines.launch


class UserActivity :BaseActivity<ActivityUserBinding>() {
    private val viewModel by lazy{UserViewModel()}
    var code:Int?=null
    var index=0
    override fun initData() {
        lifecycleScope.launch {
            viewModel.observerState.collect{
                when(it){
                    is UserState.LoginSuccess->{
                        Toast.makeText(this@UserActivity, "登录成功", Toast.LENGTH_SHORT).show()
                        RetrofitManager.token=it.entity.token
                        ARouter.getInstance().build("/user/main").navigation()
                        finish()
                    }
                    else->{

                    }
                }
            }
        }
    }

    override fun initView() {
     binding.requireCode.setOnClickListener {
         code= (Math.random()*(999999-100000+1)+100000).toInt()
         binding.loginPassword.setText("$code")
         Toast.makeText(this, "$code", Toast.LENGTH_SHORT).show()
         val ofInt = ValueAnimator.ofInt(60, 0)
         ofInt.duration=60000
         ofInt.interpolator= LinearInterpolator()
         ofInt.addUpdateListener {
             val index=it.animatedValue as Int
             if(index!=-1){
                 binding.requireCode.text="$index"
             }
         }
         ofInt.addListener(object : AnimatorListenerAdapter(){
             override fun onAnimationStart(animation: Animator) {
                 super.onAnimationStart(animation)
                 binding.requireCode.isEnabled=false
                 binding.requireCode.setBackgroundColor(Color.GRAY)
             }
             override fun onAnimationEnd(animation: Animator) {
                 super.onAnimationEnd(animation)
                 binding.requireCode.isEnabled=true
                 binding.requireCode.text="获取验证码"
                 binding.requireCode.setBackgroundColor(Color.YELLOW)

             }
         })
         ofInt.start()
     }
        binding.loginButton.setOnClickListener{
            if(binding.loginPassword.text.toString()!=code.toString()){
                index++
                Toast.makeText(this, "验证码错误", Toast.LENGTH_SHORT).show()
                if(index==3){
                    Toast.makeText(this, "输入密码错误3次，3分钟之内不能再登录", Toast.LENGTH_SHORT).show()
                }
            }else{
                lifecycleScope.launch {
                    viewModel.channel.send(UserIntent.Login("1","1"))
                }
            }


        }
    }

    override fun getLayoutId(): Int =R.layout.activity_user

}